package com.tg.app.activity.device.settings;

import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tg.app.activity.base.DeviceSettingsBaseActivity;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.app.adapter.SettingAdapter;
import com.tg.data.bean.DeviceSettingsInfo;
import com.appbase.custom.base.SettingData;
import com.tg.app.R;
import com.appbase.custom.constant.RecordCheckConstants;
import com.appbase.custom.constant.CommonConstants;
import com.tange.base.toolkit.PreferenceUtil;

import java.util.ArrayList;


/**
 * 录像查看方式
 */
public class RecordCheckActivity extends DeviceSettingsBaseActivity {

    private static final String TAG = "RecordCheckActivity";

    public static int flag = RecordCheckConstants.RECORD_BY_SDCARD;

    private ArrayList<SettingData> datas = new ArrayList<>();
    private SettingAdapter adapter ;
    private DeviceSettingsInfo  info ;

    @Override
    protected void onSetupFailed() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_record_check);
        hideActionBar();
        info = getIntent().getParcelableExtra(DeviceSettingsActivity.EXT_DEVICE_INFO);
        initView();
        backClickEvent();
    }

    public void initView(){
        RelativeLayout toolbar = findViewById(R.id.toolbar);
        TextView title = toolbar.findViewById(R.id.device_name);
        title.setText(getString(R.string.record_check_style));
        ListView listView = findViewById(R.id.record_check_list);
        int position = PreferenceUtil.getInt(getBaseContext(), CommonConstants.PRE_PLAYBACK_TYPE_X +info.deviceID);
        SettingData data = null,data1 = null;
        if(position ==  RecordCheckConstants.RECORD_BY_SDCARD){
            data1 = new SettingData(getString(R.string.cloud_record),0);
            data = new SettingData(getString(R.string.sdcard_record),1);
        }else if ( position ==  RecordCheckConstants.RECORD_BY_CLOUD){
            data1 = new SettingData(getString(R.string.cloud_record),1);
            data = new SettingData(getString(R.string.sdcard_record),0);
        }
        datas.add(data1);
        datas.add(data);

        adapter = new SettingAdapter(datas,this);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener((parent, view, position1, id) -> {
            setFlag(position1);
            if(position1 == 1){
                flag = RecordCheckConstants.RECORD_BY_SDCARD;
            }else if(position1 == 0){
                flag = RecordCheckConstants.RECORD_BY_CLOUD;
            }
            PreferenceUtil.setInt(getBaseContext(), CommonConstants.PRE_PLAYBACK_TYPE_X +info.deviceID,flag);
        });

    }

    private void setFlag(int position){
        for(int i = 0;i<datas.size();i++){
            if(i == position){
                datas.get(i).setFlag(1);
            }else{
                datas.get(i).setFlag(0);
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
